package com.antrain.his.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.antrain.his.annotation.UserLoginToken;
import com.antrain.his.entity.User;
import com.antrain.his.service.IUserService;
import com.antrain.his.utils.InitUtil;
import com.antrain.his.utils.Result;
import com.antrain.his.utils.ResultGenerator;
import com.antrain.his.utils.ShaUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author antrain
 * @since 2020-09-09
 */
@RestController
@RequestMapping("/users")
public class UserController {

  @Resource
  private IUserService userService;

  @GetMapping
  public Result getList(@RequestParam Map<String, Object> param) {
    InitUtil.initPage(param);
    int num = Integer.parseInt(param.get("page").toString());
    int limit = Integer.parseInt(param.get("limit").toString());
    QueryWrapper<User> wrapper = new QueryWrapper<User>();
    InitUtil.initEq(param, wrapper, "UserName");
    IPage<User> page = new Page<>(num, limit);// 参数一是当前页，参数二是每页个数
    return ResultGenerator.getSuccessResult(userService.page(page, wrapper));
  }

  @GetMapping("/userType/{id}")
  public Result getUserTypeList(@PathVariable int id) {
    QueryWrapper<User> wrapper = new QueryWrapper<User>();
    wrapper.eq("UseType", id);
    return ResultGenerator.getSuccessResult(userService.list(wrapper));
  }

  @GetMapping("/{id}")
  public Result getUser(@PathVariable int id) {
    User user = userService.getById(id);
    if (user == null)
      return ResultGenerator.getFailResult("", "无用户记录");
    return ResultGenerator.getSuccessResult(user);
  }

  @GetMapping("/check")
  public Result checkUserName(@RequestParam String name) {
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.eq("UserName", name);
    if (userService.getOne(wrapper) != null)
      return ResultGenerator.getFailResult("", "该用户名已存在");
    return ResultGenerator.getSuccessResult();
  }

  @PostMapping()
  @UserLoginToken
  public Result save(@RequestBody User user) {
    // System.out.println(user);
    if (!StringUtils.isEmpty(user.getPassword())) {
      user.setPassword(ShaUtil.getSHA256(user.getPassword()));
    } else {
      user.setPassword(ShaUtil.getSHA256("123456"));
    }

    if (userService.save(user))
      return ResultGenerator.getSuccessResult("", "添加成功");
    return ResultGenerator.getFailResult("", "添加失败");
  }

  @PutMapping("/{id}")
  @UserLoginToken
  public Result update(@RequestBody User user, @PathVariable int id) {
    user.setId(id);
    // System.out.println(user);
    if (!StringUtils.isEmpty(user.getPassword())) {
      user.setPassword(ShaUtil.getSHA256(user.getPassword()));
    }
    if (userService.updateById(user))
      return ResultGenerator.getSuccessResult("", "更新成功");
    return ResultGenerator.getFailResult("", "更新失败");
  }

  @DeleteMapping("/{id}")
  @UserLoginToken
  public Result del(@PathVariable int id) {
    if (userService.removeById(id))
      return ResultGenerator.getSuccessResult("", "删除成功");
    return ResultGenerator.getFailResult("", "删除失败");
  }

  @DeleteMapping("/batchdel")
  @UserLoginToken
  public Result batchDel(@RequestParam String ids) {
    String[] idList = ids.split(",");
    List<Integer> list = new ArrayList<>(idList.length);
    for (String id : idList) {
      list.add(Integer.parseInt(id));
    }
    if (userService.removeByIds(list))
      return ResultGenerator.getSuccessResult("", "删除成功");
    return ResultGenerator.getFailResult("", "删除失败");
  }

}
